package org.qydjk.console.controller.basic;

import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.Config;
import org.qydjk.common.pojo.ConfigCategory;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.util.RedisTemplateUtil;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/configManage")
public class ConfigManageController extends BaseController {
    @Autowired
    private RedisTemplateUtil redisTemplateUtil;
    /**
     * 页面跳转
     *
     * @return
     */
    @RequestMapping("/configManage.do")
    public String configManage() {
        return "basic/configManage/configManage";
    }

    /**
     * 查询配置列表信息
     *
     * @param page
     * @param rows
     * @param sort
     * @param order
     * @return
     * @throws Exception
     */
    @RequestMapping("/dataGrid.do")
    @ResponseBody
    public Object dataGrid(Integer page, Integer rows, String sort, String order, String statusType, String configCategoryId, String keySearchWord, String valueSearchWord) throws Exception {
        try {
            HibernatePage pager = new HibernatePage(page, rows);
            StringBuilder sb = new StringBuilder();
            sb.append("FROM Config c WHERE 1 = 1 ");
            if (!StringUtil.isEmptyOrNull(statusType)) {
                sb.append(" AND c.status = " + statusType);
            }
            if (!StringUtil.isEmptyOrNull(configCategoryId)) {
                sb.append(" AND c.configCategoryId = " + configCategoryId);
            }
            if (!StringUtil.isEmptyOrNull(keySearchWord)) {
                sb.append(" AND c.k LIKE '%" + keySearchWord + "%'");
            }
            if (!StringUtil.isEmptyOrNull(valueSearchWord)) {
                sb.append(" AND c.v LIKE '%" + valueSearchWord + "%'");
            }
            sb.append(" order by c." + sort + " " + order);


            StringBuilder countSb = new StringBuilder();
            countSb.append("select count(c.id) from Config c WHERE 1 = 1 ");
            if (!StringUtil.isEmptyOrNull(statusType)) {
                countSb.append(" AND c.status = " + statusType);
            }
            if (!StringUtil.isEmptyOrNull(configCategoryId)) {
                countSb.append(" AND c.configCategoryId = " + configCategoryId);
            }
            if (!StringUtil.isEmptyOrNull(keySearchWord)) {
                countSb.append(" AND c.k LIKE '%" + keySearchWord + "%'");
            }
            if (!StringUtil.isEmptyOrNull(valueSearchWord)) {
                countSb.append(" AND c.v LIKE '%" + valueSearchWord + "%'");
            }
            String hql = sb.toString();
            List<Config> list = baseService.findByHql(hql, pager);
            int total = baseService.getRecordCountHQL(countSb.toString());
            return new EasyUIResponse(total, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 添加配置页
     *
     * @return
     */
    @GetMapping("/addPage.do")
    public String addPage() {
        return "basic/configManage/configManageAdd";
    }

    /**
     * 添加配置
     *
     * @param config
     * @return
     */
    @PostMapping("/add.do")
    @ResponseBody
    public Object add(@Valid Config config) throws Exception {
        try {
            Date date = new Date();
            config.setCreateTime(date);
            config.setUpdateTime(date);
            baseService.save(config);
            return renderSuccess("添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除配置
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public Object delete(Integer id) throws Exception {
        try {
            Config config = new Config();
            config.setId(id);
            baseService.delete(config);
            return renderSuccess("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 编辑配置
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editPage.do")
    public String editPage(Model model, Integer id) throws Exception {
        try {
            Config config = baseService.get(Config.class, id);
            model.addAttribute("config", config);
            return "basic/configManage/configManageEdit";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 编辑配置
     *
     * @param config
     * @return
     */
    @RequestMapping("/edit.do")
    @ResponseBody
    public Object edit(@Valid Config config) throws Exception {
        try {
            Config c = baseService.get(Config.class, config.getId());
            BeanUtil.copyProperties(config,c);
            c.setUpdateTime(new Date());
            baseService.update(c);
            return renderSuccess("编辑成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return renderError("编辑失败");
        }
    }

    /**
     * 查询配置分类信息
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/findConfigCategory.do")
    @ResponseBody
    public Object findConfigCategory() throws Exception {
        try {
            List<ConfigCategory> configCategoryList = baseService.findByHql("FROM ConfigCategory");
            return configCategoryList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/addToRedis.do")
    @ResponseBody
    public Object addToRedis(){
        try {
            String findConfigSql = "SELECT c.k,c.v  " +
                    " FROM qy_config c " +
                    " WHERE c.`status` = 1";
            List<Map<String, Object>> list = baseService.findMapBySQL(findConfigSql);
            redisTemplateUtil.setWithByte(list);
            return  renderSuccess("同步成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return  renderError("同步失败!");
        }
    }

}
